package com.apexsoft.front.common.utils.session;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONObject;

public class UserSessionNoRedis {

	/**
	 * 控制台会话ID
	 */
	public static final String CONSOLE_SESSION = "USER_SESSION_CONSOLE";
	/**
	 * 获取对应系统的session对象
	 * @param request
	 * @param sessionKey
	 * @return
	 */
	public static JSONObject getSessionObj(HttpServletRequest request,String sessionKey)
	{
		HttpSession session = request.getSession(false);
		if(session!=null){
			if(session.getAttribute(sessionKey)!=null){
				JSONObject sessionObj = (JSONObject)session.getAttribute(sessionKey);
				return sessionObj;
			}
			else{
				return null;
			}
		}
		else{
			return null;
		}
		
	}
	
	/**
	 * 判断对应系统是否存在登录会话
	 * @param request
	 * @param sessionKey
	 * @return
	 */
	public static boolean isLogin(HttpServletRequest request,String sessionKey)
	{
		if(getSessionObj(request,sessionKey)!=null){
			return true;
		}
		else{
			return false;
		}
	}
	
	/**
	 * 设置对应系统的session对象
	 * @param request
	 * @param sessionKey
	 * @param sessionObj
	 */
	public static void setSessionObj(HttpServletRequest request,String sessionKey,JSONObject sessionObj)
	{
		HttpSession session = request.getSession();
		session.setAttribute(sessionKey, sessionObj);
	}
	
	/**
	 * 删除对应系统的登录会话
	 * @param request
	 * @param sessionKey
	 */
	public static void removeSession(HttpServletRequest request,String sessionKey) {
		HttpSession session = request.getSession(false);
		if(session!=null){
			session.removeAttribute(sessionKey);
		}
	}
}
